/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;

/**
 *
 * @author Tiago
 */
public class TelaVerProvas extends javax.swing.JFrame {

    /**
     * Creates new form TelaVerProvas
     */
    
    private String prova;
    private  boolean  gabarito;
    
    public TelaVerProvas(String prova, boolean gabarito) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.prova = prova;
        this.gabarito = gabarito;
        txtListaProvaGerada.setText(prova);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtListaProvaGerada = new javax.swing.JTextArea();
        btnSalvarProva = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Visualizar Documento");
        setResizable(false);

        txtListaProvaGerada.setColumns(20);
        txtListaProvaGerada.setEditable(false);
        txtListaProvaGerada.setRows(5);
        txtListaProvaGerada.setText("lista a prova gerada");
        jScrollPane1.setViewportView(txtListaProvaGerada);

        btnSalvarProva.setText("Salvar");
        btnSalvarProva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarProvaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 282, Short.MAX_VALUE)
                .addComponent(btnSalvarProva, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(249, 249, 249))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSalvarProva)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarProvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarProvaActionPerformed

        try {
            File f ;
            if (gabarito) {
                f = new File("gabarito.txt");
            }else{
                f = new File("prova.txt");
            }
            f.delete();
            FileWriter fr = new FileWriter(f, true);
            PrintWriter out = new PrintWriter(fr);
            out.println(prova);
            out.close();
            mostraMensagem("Prova salva!");
        } catch (IOException e) {
            mostraMensagem("Erro ao escrever arquivo.");
        }
    }//GEN-LAST:event_btnSalvarProvaActionPerformed

    private void mostraMensagem(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem, "Gerar Provas", JOptionPane.INFORMATION_MESSAGE);
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvarProva;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtListaProvaGerada;
    // End of variables declaration//GEN-END:variables
}
